package com.swz.dcrm.ui.home;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.swz.dcrm.adpter.ClueSelectAdapter;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Clue;
import com.swz.dcrm.model.ClueRecord;
import com.swz.dcrm.model.Page;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.CarManagementViewModel;
import com.swz.dcrm.ui.viewmodel.CustomerManagementViewModel;
import com.swz.dcrm.ui.viewmodel.TodayPlanViewModel;
import com.swz.dcrm.util.CustomDecoration;
import com.swz.dcrm.wrapper.EmptyWrapper;
import com.xh.baselibrary.callback.OnClickListener;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClueSelectFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private ClueSelectAdapter clueSelectAdapter;
    @Inject
    CarManagementViewModel carManagementViewModel;
    private EmptyWrapper emptyWrapper;
    private int page=1;
    private int size=15;
    private long total;

    @Inject
    CustomerManagementViewModel customerManagementViewModel;
    public static ClueSelectFragment newInstance() {

        return new ClueSelectFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())){
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
     customerManagementViewModel.getUsableClue(page,size).observe(this,observer );
    }
    Observer observer=new Observer<BaseResponse<Page<Clue>>>() {
        @Override
        public void onChanged(BaseResponse<Page<Clue>> pageBaseResponse) {
                if(pageBaseResponse.isSuccess()){
                    total=pageBaseResponse.getData().getTotal();
                    if(clueSelectAdapter==null){
                        clueSelectAdapter=new ClueSelectAdapter(getContext(),pageBaseResponse.getData().getList());
                        clueSelectAdapter.setOnClickListener(clueRecord -> {
                            carManagementViewModel.getCurrentSelectedClueRecord().setValue(clueRecord);
                            backClick();
                        });
                        emptyWrapper=getEmptyWrapper(clueSelectAdapter,R.mipmap.empty_client);
                        rv.setAdapter(emptyWrapper);
                    }else {
                        clueSelectAdapter.refresh(pageBaseResponse.getData().getPageNum(),pageBaseResponse.getData().getList());
                        emptyWrapper.notifyDataSetChanged();
                    }
                }
        }
    };

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_clue_select;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        title.setText(getString(R.string.approval_select_clue));
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        rv.addItemDecoration(new CustomDecoration(getContext(),5,5,15,15));
        smartRefreshLayout.setOnRefreshListener(refreshlayout -> {
           page=1;
           smartRefreshLayout.setEnableLoadmore(true);
           onLoadRetry();
        });
        smartRefreshLayout.setOnLoadmoreListener(refreshlayout -> {
           if(clueSelectAdapter.getItemCount()>=total){
               smartRefreshLayout.finishLoadmore();
               smartRefreshLayout.setEnableLoadmore(false);
               return;
           }
           page++;
           onLoadRetry();
        });
        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
}
